﻿using System.Data.Entity.Design.PluralizationServices;
using System.Globalization;
using System.Linq;
using DBBuilder.DBDataStructure;
using AppsGenerator.Models;

namespace AppsGenerator.Utils
{
    /// <summary>
    /// Class for implementing extension methods
    /// </summary>
    public static class ExtensionMethods
    {
        /// <summary>
        /// Returns the plural form of the specified word
        /// </summary>
        /// <param name="s">The word to be made plural.</param>
        /// <returns>A System.String that is the plural form of the input parameter.</returns>
        public static string Pluralize(this string s)
        {
            return PluralizationService.CreateService(CultureInfo.CurrentCulture).Pluralize(s);
        }

        /// <summary>
        /// Returns the singular form of the specified word.
        /// </summary>
        /// <param name="s">The the word to be made singular.</param>
        /// <returns>The singular form of the input parameter.</returns>
        public static string Singularize(this string s)
        {
            return PluralizationService.CreateService(CultureInfo.CurrentCulture).Singularize(s);
        }

        /// <summary>
        /// Return the word with the first character as lower case
        /// </summary>
        /// <param name="s">The the word to be made conversion</param>
        /// <returns></returns>
        public static string ToLowerFirstCharacter(this string s)
        {
            return string.IsNullOrEmpty(s) ? s : s[0].ToString().ToLower() + s.Substring(1);
        }

        /// <summary>
        /// Checking whether table is configured as user table
        /// </summary>
        /// <param name="tbl"></param>
        /// <returns></returns>
        public static bool IsUserTable(this Table tbl)
        {
            return tbl.Name.Equals(WebConfiguration.Instance.Settings.UserTable,
                                   System.StringComparison.OrdinalIgnoreCase);
        }

        /// <summary>
        /// Determines whether table is configured with any of image attributes 
        /// </summary>
        /// <param name="tbl"></param>
        /// <param name="ImageAttr"></param>
        /// <returns></returns>
        public static bool IsTableWithImage(this Table tbl, out string ImageAttr)
        {
            var item = WebConfiguration.Instance.ConfigTables.FirstOrDefault(n => n.Name.Equals(tbl.Name, System.StringComparison.OrdinalIgnoreCase) &&
                                                                    !string.Empty.Equals(n.AttributeImage));
            var result = false;
            ImageAttr = string.Empty;
            if (item != null)
            {
                ImageAttr = item.AttributeImage;
                result = true;
            }
            return result;
        }

        /// <summary>
        /// Determines whether table is configured with FCK Editor
        /// </summary>
        /// <param name="tbl"></param>
        /// <param name="AttributeFCK"></param>
        /// <returns></returns>
        public static bool IsTableWithFCK(this Table tbl, out string AttributeFCK)
        {
            var item = WebConfiguration.Instance.ConfigTables.FirstOrDefault(n => n.Name.Equals(tbl.Name, System.StringComparison.OrdinalIgnoreCase) &&
                                                                    !string.Empty.Equals(n.AttributeFCK));
            AttributeFCK = string.Empty;
            var result = false;
            if (item != null)
            {
                AttributeFCK = item.AttributeFCK;
                result = true;
            }
            return result;
        }

        /// <summary>
        /// Determines whether table is configured with AJAX
        /// </summary>
        /// <param name="tbl"></param>
        /// <returns></returns>
        public static bool IsTableWithAjax(this Table tbl)
        {
            var item = WebConfiguration.Instance.ConfigTables.FirstOrDefault(n => n.Name.Equals(tbl.Name, System.StringComparison.OrdinalIgnoreCase) &&
                                                                    !string.Empty.Equals(n.IsWithAjax) && 
                                                                    bool.Parse(n.IsWithAjax));
            return item != null;
        }

        /// <summary>
        /// Determines whether table is configured with main foreign key
        /// </summary>
        /// <param name="tbl"></param>
        /// <param name="MainForeignKey"></param>
        /// <returns></returns>
        public static bool IsTableWithMainForeignKey(this Table tbl, out string MainForeignKey)
        {
            var item = WebConfiguration.Instance.ConfigTables.FirstOrDefault(
                n => n.Name.Equals(tbl.Name, System.StringComparison.OrdinalIgnoreCase) &&
                     !string.Empty.Equals(n.MainForeignKey));

            MainForeignKey = null;
            var result = false;
            
            if (item != null)
            {
                MainForeignKey = item.MainForeignKey;
                result = true;
            }
            return result;
        }
    }
}
